# 发送post请求，模拟浏览器的登录github
import requests
import re

# 访问登录页面
r1=requests.get('https://github.com/login/',
                     headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'},

                     )

authenticity_token=re.findall(r'name="authenticity_token".*?value="(.*?)"',r1.text,re.S)[0]
# print(r1.cookies.items()) #获取元祖类型的cookies信息
# print(r1.cookies.get_dict())#获取字典类型的cokies信息
cookies=r1.cookies.get_dict()


# 访问登录页面
r2=requests.post('https://github.com/session',
    data={
    'commit':'Sign in',
    'utf8':'✓',
    'authenticity_token':authenticity_token,
    'login':'964902285@qq.com',
    'password':'*'},
    headers = {
 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'},
    cookies=cookies)


# 访问设置个人主页
cookies2=r2.cookies.get_dict() # 获取登录页面返回的cokies信息
r3=requests.get('https://github.com/settings/emails',cookies=cookies2)

print('964902285@qq.com' in r3.text)
